package com.whz.governance.sensitive;

import com.alibaba.cloud.nacos.NacosConfigAutoConfiguration;
import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.nacos.NacosConfigProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;

/**
 * 基于nacos存储的敏感词,读取和上传敏感词
 *
 * @author whz
 */
@Slf4j
@Configuration
@AutoConfigureAfter(NacosConfigAutoConfiguration.class)
@ConditionalOnProperty(name = "generic.sensitive.enable", havingValue = "true")
@ConditionalOnBean(NacosConfigAutoConfiguration.class)
public class SensitiveAutoConfiguration {

    @Bean
    public SensitiveNacosConfig sensitiveNacosConfig(
            @Autowired(required = false) final NacosConfigProperties nacosConfigProperties) {
        Assert.notNull(nacosConfigProperties, "nacosConfigProperties cannot be null");
        return new SensitiveNacosConfig(nacosConfigProperties);
    }

    @Bean
    public SensitiveStoreLoader sensitiveStoreLoader(SensitiveNacosConfig sensitiveNacosConfig) {
        return new SensitiveStoreLoader(sensitiveNacosConfig);
    }

    @Bean
    @ConditionalOnBean(NacosConfigManager.class)
    public SensitiveStoreUploader sensitiveStoreUploader(NacosConfigManager nacosConfigManager,
                                                         SensitiveNacosConfig sensitiveNacosConfig) {
        return new SensitiveStoreUploader(nacosConfigManager.getConfigService(), sensitiveNacosConfig);
    }
}